<?php
if (!defined('ROOT_PATH')) die('No direct access.');
	if (isset($param[1])){
		if ($param[1]=='bayar'){
			$invoice = (isset($param[2])) ? $param[2] : "";
			$idfb = (isset($param[3])) ? $param[3] : "";
			$idtrans = (isset($param[4])) ? $param[4] : "";
			konfBayar($invoice,$idfb,$idtrans);
			header('Location: ' . $_SERVER['HTTP_REFERER']);
			break;
		}else if ($param[1]=='kirim'){
			$pesanan = (isset($param[2])) ? $param[2] : "";
			$idfb = (isset($param[3])) ? $param[3] : "";
			$idtrans = (isset($param[4])) ? $param[4] : "";
			konfKirim($pesanan,$idfb,$idtrans);
			header('Location: ' . $_SERVER['HTTP_REFERER']);
			break;
		} else if ($param[1]=='batal'){
			$pesanan = (isset($param[2])) ? $param[2] : "";
			$idfb = (isset($param[3])) ? $param[3] : "";
			$idtrans = (isset($param[4])) ? $param[4] : "";
			//batalPesan($pesanan,$idfb,$idtrans);
			header('Location: ' . $_SERVER['HTTP_REFERER']);
			break;
		}else
		if (isset($_SESSION['logged'])){
			switch ($param[1]){
				case 'co':
					view_co($_SESSION['logged']['id']);
					break;
				case 'fin':
					//var_dump($_POST);
					$nama = amankan($_POST['nama-penerima']);
					$kota = amankan($_POST['kota']);
					$kontak = amankan($_POST['no-penerima']);
					$alamat = amankan($_POST['alamat']);
					$total = $_POST['total'];
					$diskon = $_POST['diskon'];
					$metode = $_POST['metode'];
					//
					$idPembeli = $_SESSION['logged']['index'];
					$invoice = createResi($idPembeli);
					$tgl = date("Y-m-d H:i:s");
					$idAlamat = cekAlamat($nama,$kota,$kontak,$alamat,$idPembeli);
					//masukkan
					$idTrans = addTransaksi($invoice,$tgl,$total,$diskon,$idAlamat,$idPembeli);					
					foreach ($_SESSION['keranjang'] as $key=>$barang) {
						$biaya = $_POST["kirim-$key"];
						$prod = getBarang($barang['id']);
						addPesanan($idTrans,$prod['idPenjual'],$barang['id'],$barang['kuantitas'],$metode,$biaya,$prod['preorder']);
						if ($prod['preorder']) {
							//sendSMS($nomor,$pesan);
							//sendEmail($email,$pesan,$subject,$from,$from_name)
						}
					}
					unset($_SESSION['keranjang']);
					
					//kirim email
					$uname = $_SESSION['logged']['uname'];
					$idfb = $_SESSION['logged']['id'];
					$email1 = "$uname@facebook.com";
					$email2 = $_SESSION['logged']['email'];
					$link = TRANSAKSI_URL."bayar/$invoice/$idfb/$idTrans/";
					$pesan = "INVOICE \n Sila konfirmasi pembayaran disini $link";
					$subject = "Invoice dan Konfirmasi Pesanan #$invoice";
					$from_name = "Admin Realita";
					sendEmail($email1,$pesan,$subject,$from_name);
					sendEmail($email2,$pesan,$subject,$from_name);
					
					//pesan-fb
					$_SESSION['sukses-checkout'] = "Transaksi  telah selesai. Silakan cek profil transaksi atau message FB Anda";
					//$header = "https://www.facebook.com/dialog/send?to=$uname&app_id=147346958657510&link=$link&redirect_uri=".PROFIL_URL."transaksi/";
					header("Location: ".PROFIL_URL."transaksi");
					break;
				default:
					view_404();
					break;
			}
	}else
		view_404();
	}else
		view_404();
?>
